/**
 * 
 */
package com.healthshare.activity.news;


import com.healthshare.HealthShareActivity;
import com.healthshare.R;
import com.healthshare.busi.news.NewsClassBusi;
import com.healthshare.busi.news.TitleListBusi;
import com.healthshare.parse.news.NewsClassParse;
import com.healthshare.parse.news.TitleListParse;
import com.healthshare.parse.news.NewsListParse.NewsLisItem;
import com.healthshare.parse.news.TitleListParse.TitleListItem;
import com.mobclick.android.MobclickAgent;
import com.palmedia.framework.business.BaseBusi;
import com.palmedia.framework.myinterface.UiCallBack;
import com.palmedia.framework.ui.CustomListView;
import com.palmedia.framework.ui.CustomListView.OnGetMoreListener;
import com.palmedia.framework.ui.CustomListView.OnRefreshListener;
import com.palmedia.framework.ui.CustomMessageShow;
import com.waps.AppConnect;
import com.waps.UpdatePointsNotifier;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author xudeliang
 * 文章列表界面
 */
public class ActicleListActivity extends Activity implements UiCallBack,OnGetMoreListener,OnRefreshListener,DialogInterface.OnClickListener, UpdatePointsNotifier
{
	
	CustomListView customListView;
	///ProgressBar progressBar;
	private String listId;
	int pageid=1;//
	String getPointError;
	 final Handler mHandler = new Handler();
	/**
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		//con=this;
		setContentView(R.layout.acticlelist);
		customListView =(CustomListView) findViewById(R.id.list);
		customListView.setonGetMoreListener(this);
		customListView.setonRefreshListener(this);
		customListView.setMoreTextColor(Color.parseColor("#668B8B"));
		customListView.setUpdateTextColor(Color.parseColor("#668B8B"));
		customListView.setHeadBackgrondColor(Color.parseColor("#DCDCDC"));
		customListView.setBottomBackgrondColor(Color.parseColor("#DCDCDC"));
		
		((Button)this.findViewById(R.id.backbutton)).setVisibility(View.VISIBLE);
		((Button)this.findViewById(R.id.backbutton)).setTextColor(Color.rgb(0,139,0));
		
		TextView titlecenter =(TextView) findViewById(R.id.titletextview);
		
		Bundle  bb=null;
		if(savedInstanceState!=null)
        {
        	bb=savedInstanceState;
        }
        else
        {
        	bb=this.getIntent().getBundleExtra("bundle");
        }
		listId = bb.getString("listid");
		String title = bb.getString("title");
		titlecenter.setText(title);
		customListView.setPageSize(TitleListBusi.pagesize);
		customListView.freshData();
		customListView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3)
			{
				final TitleListItem nli = (TitleListItem)v.getTag();
				String point =  MobclickAgent.getConfigParams(ActicleListActivity.this, "point");
				if(null!=point && point.equals("true") &&nli.paytype==1)
		    	{
					if(HealthShareActivity.point>=HealthShareActivity.consumePoint)
					{
						CustomMessageShow.getInst().showConfirmDialog(ActicleListActivity.this, "阅读本内容需要扣除"+HealthShareActivity.consumePoint+"积分，您当前积分为："+HealthShareActivity.point+"，建议阅读后收藏，以便下次免积分阅读。", "扣除积分提醒", "扣除阅读", "免费获积分","取消", 0, new DialogInterface.OnClickListener(){
							@Override
							public void onClick(DialogInterface dialog, int which)
							{
								// TODO Auto-generated method stub
								if(which==DialogInterface.BUTTON_POSITIVE)
								{
									showArticle(nli.articleid );
								   	AppConnect.getInstance(ActicleListActivity.this).spendPoints(HealthShareActivity.consumePoint, ActicleListActivity.this);
								}
								else if(which==DialogInterface.BUTTON_NEUTRAL)
								{
									AppConnect.getInstance(ActicleListActivity.this).showOffers(ActicleListActivity.this);
								}
							}
							
						});
						
						
					}
					else
					{
						CustomMessageShow.getInst().showConfirmDialog(ActicleListActivity.this, "阅读本内容需要"+HealthShareActivity.consumePoint+"积分，您当前积分为："+HealthShareActivity.point, "积分不足", "免费获取积分",null, "取消", 0, ActicleListActivity.this);
					}
		    	}
		    	else
		    	{
		    		showArticle(nli.articleid );
		    	}
				
				
			}
    		
    	});
		//reqActileListBusi(1);
		//customListView.retsetStatus();
	}
	
	private void showArticle(String articleid )
	{
		Bundle bundle=new Bundle();
		Intent intent=new Intent();
		intent.putExtra("articlebundle",bundle);
		//TitleListItem nli = (TitleListItem)v.getTag();
		
		bundle.putString("articleid",articleid);
	   	intent.setClass(this,ActicleActivity.class);
	   	this.startActivity(intent);
	}
	public void backButtonAction(View view)
	{
		this.finish();
	}
	/**
	 * @author xudeliang
	 * 请求文章列表数据
	 */
	private void reqActileListBusi(int page)
	{
		TitleListBusi  ncb = new TitleListBusi(this);
		ncb.page=page+"";
		ncb.id = listId;
		ncb.iExecute();
		//progressBar.setVisibility(View.VISIBLE);
		
	}


	/* (non-Javadoc)
	 * @see com.palmedia.framework.myinterface.UiCallBack#uiCallBack(com.palmedia.framework.business.BaseBusi)
	 */
	@Override
	public void uiCallBack(BaseBusi obj)
	{
		// TODO Auto-generated method stub
		
		if(obj instanceof TitleListBusi)
		{
			//progressBar.setVisibility(View.GONE);
			TitleListParse tlb = (TitleListParse)obj.getBaseStruct();
			
			if(null ==tlb ||tlb.titleLisItems.size()<=0)
			{
				if ("1".equals(tlb.page))// 根据回来的哦数据刷新界面
				{
					customListView.onRefreshComplete();
				}
				else
				{
					customListView.onGetMoreComplete();
				}
				
				Toast.makeText(this, "无列表数据，请重试。", Toast.LENGTH_SHORT).show();
				return;
			}
			actileListBusiCallBack(tlb);
			
		}
	}
	
	
	private View buildRowView(TitleListItem tt)
	{
		View view = View.inflate(this, R.layout.acticlelistrow, null);
		TextView title = (TextView)view.findViewById(R.id.title);
		TextView time = (TextView)view.findViewById(R.id.time);
		title.setText(tt.title);
		title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		time.setText(tt.time);
		title.setTextColor(Color.parseColor("#8968CD"));
		time.setTextColor(Color.parseColor("#8968CD"));
		view.setTag(tt);
		/*view.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				Bundle bundle=new Bundle();
				Intent intent=new Intent();
				intent.putExtra("bundle",bundle);
				TitleListItem nli = (TitleListItem)v.getTag();
				
				bundle.putString("acticleid",nli.acticleid);
			   	intent.setClass(con,ActicleActivity.class);
			   	con.startActivity(intent);
			}
			
		});*/
		
		return view;
	}
	
	
	//回调刷新界面
	private void actileListBusiCallBack(TitleListParse cdp)
	{
		
		if ("1".equals(cdp.page))// 刷新
		{// 根据回来的哦数据刷新界面
			customListView.removeAllView();
		}
		for(int i = 0;i < cdp.titleLisItems.size();i++)
		{
			View v = buildRowView(cdp.titleLisItems.get(i));
			if (v != null)
			{
				customListView.addViewToLast(v);
			}
		}
		if ("1".equals(cdp.page))// 根据回来的哦数据刷新界面
		{
			customListView.onRefreshComplete();
		}
		else
		{
			customListView.onGetMoreComplete();
		}
		int faction = customListView.getListNum()%TitleListBusi.pagesize;
		if(0==faction)
		{
			customListView.setBottomViewVisible(true);
		}
		else
		{
			customListView.setBottomViewVisible(false);
		}
		
	}
	
	@Override
	public void onGetmore()
	{
		int num = customListView.getListNum();
		int pagenum =(num/TitleListBusi.pagesize)+1;
		reqActileListBusi(pagenum);
	
	}


	/* (non-Javadoc)
	 * @see com.palmedia.framework.ui.CustomListView.OnRefreshListener#onRefresh()
	 */
	@Override
	public void onRefresh()
	{
		reqActileListBusi(1);
		// TODO Auto-generated method stub
	}

	/* (non-Javadoc)
	 * @see android.content.DialogInterface.OnClickListener#onClick(android.content.DialogInterface, int)
	 */
	@Override
	public void onClick(DialogInterface dialog, int which)
	{
		// TODO Auto-generated method stub
		if(which==DialogInterface.BUTTON_POSITIVE)
		{
			AppConnect.getInstance(this).showOffers(this);
		}
	}
	 
	 
	@Override
	public void onResume() 
	{
	    // 返回结果在回调函数getUpdatePoints(...)中处理
		AppConnect.getInstance(this).getPoints(this);
	    super.onResume();
	}
	 
	/* (non-Javadoc)
	 * @see com.waps.UpdatePointsNotifier#getUpdatePoints(java.lang.String, int)
	 */
	@Override
	public void getUpdatePoints(String arg0, int arg1)
	{
		// TODO Auto-generated method stub
		HealthShareActivity.point=arg1;
	}

	/* (non-Javadoc)
	 * @see com.waps.UpdatePointsNotifier#getUpdatePointsFailed(java.lang.String)
	 */
	@Override
	public void getUpdatePointsFailed(String arg0)
	{
		getPointError=arg0;
		mHandler.post(mUpdateResults);
		//CustomMessageShow.getInst().showShortToast(this,  arg0);
		
	}
	// 创建一个线程
	final Runnable mUpdateResults = new Runnable() {
		public void run() {
			if (getPointError != null) {
				CustomMessageShow.getInst().showShortToast(ActicleListActivity.this,  getPointError);
			}
		}
	};
}
